package com.test.util.helper;

import java.util.List;

/**
 * ClassName:Page
 * Package:com.test.ssm.article.util.helper
 * Description:
 *
 * @Date:2021/12/2 20:53
 * @Author:yang
 */
public class MyPage<T> {
    //当前页
    private int pageNum;
    //每页条目
    private int pageSize;
    //总记录数
    private long count;
    //总页数
    private int pages;
    //记录
    private List<T> list;
    //前一页
    private int prePage;
    //后一页
    private int nextPage;
    //是否为首页
    private boolean isFirstPage;
    //是否为尾页
    private boolean isLastPage;
    //是否有前一页
    private boolean hasPrePage = true;
    //是否有后一页
    private boolean hasNextPage = true;
    //导航页
    private int[] navigatePageNums;
    //导航第一页
    private int navigateFirstPage;
    //导航最后页
    private int navigateLastPage;

    public MyPage(int pageNum, int pageSize, long count, int navigatePages, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.count = count;
        this.list = list;
        this.pages = (int) (count / pageSize) + (count % pageSize != 0 ? 1 : 0);
        if (pageNum == 1) {
            isFirstPage = true;
            hasPrePage = false;
            if (pages == 1) {
                isLastPage = true;
                hasNextPage = false;
            }
        } else if (pageNum == pages) {
            isLastPage = true;
            hasNextPage = false;
        }
        prePage = pageNum - 1;
        nextPage = pageNum + 1;
        if(pageNum == 1){
            prePage = 1;

        }
        if(pageNum == pages){
            nextPage = pages;
        }
        if (pages <= navigatePages) {
            navigateFirstPage = 1;
            navigateLastPage = pages;
            navigatePageNums = new int[pages];
            for (int i = 1; i <= pages; i++) {
                navigatePageNums[i - 1] = i;
            }
        } else if (pageNum < navigatePages / 2) {
            navigateFirstPage = 1;
            navigateLastPage = navigatePages;
            navigatePageNums = new int[navigatePages];
            for (int i = 0; i < navigatePages; i++) {
                navigatePageNums[i] = i + 1;
            }
        } else {
            navigateFirstPage = pageNum - navigatePages / 2;
            navigateLastPage = pageNum - navigatePages / 2 + navigatePages - 1;
            navigatePageNums = new int[navigatePages];
            for (int i = 0; i < navigatePages; i++) {
                navigatePageNums[i] = pageNum - navigatePages / 2 + i;
            }
        }
    }

    public int getPageNum() {
        return pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public long getCount() {
        return count;
    }

    public int getPages() {
        return pages;
    }

    public List<T> getList() {
        return list;
    }

    public int getPrePage() {
        return prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public boolean isIsFirstPage() {
        return isFirstPage;
    }

    public boolean isIsLastPage() {
        return isLastPage;
    }

    public boolean isHasPrePage() {
        return hasPrePage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public int[] getNavigatePageNums() {
        return navigatePageNums;
    }

    public int getNavigateFirstPage() {
        return navigateFirstPage;
    }

    public int getNavigateLastPage() {
        return navigateLastPage;
    }
}
